from bs4 import BeautifulSoup
import datetime

#   获取百家欧指
def getouzhi(bs,fid,matchNo):
    # 列表数据
    datatb = bs.find("table", attrs={"id": "datatb"})
    trs = datatb.find_all("tr", recursive=False)
    pslist = []
    for tr in trs:
        # print(":",tr)
        # id 代表的是每一博彩公司
        id = tr["id"]
        psm = {"id":id}
        if id == "step_line":
            break

        tds = tr.find_all("td", recursive=False)
        pocainame = tds[1].get("title")
        psm["pocainame"] =pocainame
        # 胜平负赔率
        spf_trs = tds[2].find_all("tr")
        # 初赔
        spf_start_tds = spf_trs[0].find_all("td")
        # 胜
        shp = spf_start_tds[0].text
        # 平
        sdp = spf_start_tds[1].text
        # 负
        sap = spf_start_tds[2].text

        # 即赔
        spf_end_tds = spf_trs[1].find_all("td")
        # 胜
        ehp = spf_end_tds[0].text
        # 平
        edp = spf_end_tds[1].text
        # 负
        eap = spf_end_tds[2].text
        psm["spf" ] ={"s" :[shp ,sdp ,sap] ,"e" :[ehp ,edp ,eap]}
        # 即时概率
        jisp_trs = tds[3].find_all("tr")
        # 初
        jisp_start_tds = jisp_trs[0].find_all("td")
        # 胜
        jisp_s_h = jisp_start_tds[0].text
        # 平
        jisp_s_d = jisp_start_tds[1].text
        # 负
        jisp_s_a = jisp_start_tds[2].text

        # 即
        jisp_end_tds = jisp_trs[1].find_all("td")
        # 胜
        jisp_e_h = jisp_end_tds[0].text
        # 平
        jisp_e_d = jisp_end_tds[1].text
        # 负
        jisp_e_a = jisp_end_tds[2].text
        psm["gailv"] = {"s": [jisp_s_h, jisp_s_d, jisp_s_a], "e": [jisp_e_h, jisp_e_d, jisp_e_a]}

        # 返还率
        fan_trs = tds[4].find_all("tr")
        # 初
        fan_start_tds = fan_trs[0].find_all("td")
        fan_s = fan_start_tds[0].text
        # 即
        fan_end_tds = fan_trs[1].find_all("td")
        fan_e = fan_end_tds[0].text
        psm["fanhan"] = {"s": fan_s, "e": fan_e}

        # 即时凯利
        kaili_table = tds[4].find("td",recursive=False).find("table")
        kaili_trs = kaili_table.find_all("tr")
        # print(kaili_table)
        # 初
        kaili_start_tds_str = kaili_trs[0]
        kaili_start_tds = kaili_start_tds_str.find_all("td")
        # 胜
        kaili_s_h = kaili_start_tds[0].text
        # 平
        kaili_s_d = kaili_start_tds[1].text
        # 负
        kaili_s_a = kaili_start_tds[2].text

        # 即
        kaili_end_tds = kaili_trs[1].find_all("td")
        # 胜
        kaili_e_h = kaili_end_tds[0].text
        # 平
        kaili_e_d = kaili_end_tds[1].text
        # 负
        kaili_e_a = kaili_end_tds[2].text

        psm["kaili" ] ={"s" :[kaili_s_h ,kaili_s_d ,kaili_s_a] ,"e" :[kaili_e_h ,kaili_e_d ,kaili_e_a]}
        pslist.append(psm)

    # 统计数据
    table_btm = bs.find("div", attrs={"id": "table_btm"})
    table_btm_table = table_btm.find("table")
    btm_trs = table_btm_table.find_all("tr", attrs={"xls": "footer"})
    # print(btm_trs.prettify())
    # 平均欧赔
    btm_tds = btm_trs[0].find_all("td" ,recursive=False)
    btm_spf_trs = btm_tds[2].find("table").find_all("tr")

    # 初赔
    btm_spf_start_tds = btm_spf_trs[0].find_all("td")
    # 胜
    btm_shp = btm_spf_start_tds[0].text
    # 平
    btm_sdp = btm_spf_start_tds[1].text
    # 负
    btm_sap = btm_spf_start_tds[2].text

    # 即赔
    btm_spf_end_tds = btm_spf_trs[1].find_all("td")
    # 胜
    btm_ehp = btm_spf_end_tds[0].text
    # 平
    btm_edp = btm_spf_end_tds[1].text
    # 负
    btm_eap = btm_spf_end_tds[2].text

    # 即时概率
    btm_jisp_trs = btm_tds[3].find_all("tr")

    # 初
    btm_jisp_start_tds = btm_jisp_trs[0].find_all("td")
    # 胜
    btm_jisp_s_h = btm_jisp_start_tds[0].text
    # 平
    btm_jisp_s_d = btm_jisp_start_tds[1].text
    # 负
    btm_jisp_s_a = btm_jisp_start_tds[2].text

    # 即
    btm_jisp_end_tds = btm_jisp_trs[1].find_all("td")
    # 胜
    btm_jisp_e_h = btm_jisp_end_tds[0].text
    # 平
    btm_jisp_e_d = btm_jisp_end_tds[1].text
    # 负
    btm_jisp_e_a = btm_jisp_end_tds[2].text

    # 返还率
    btm_fan_trs = btm_tds[4].find_all("tr")
    # 初
    btm_fan_start_tds = btm_fan_trs[0].find_all("td")
    btm_fan_s = btm_fan_start_tds[0].text
    # 即
    btm_fan_end_tds = btm_fan_trs[1].find_all("td")
    btm_fan_e = btm_fan_end_tds[0].text

    # 即时凯利
    btm_kaili_trs = btm_tds[5].find_all("tr")
    # 初
    btm_kaili_start_tds = btm_kaili_trs[0].find_all("td")
    # 胜
    btm_kaili_s_h = btm_kaili_start_tds[0].text
    # 平
    btm_kaili_s_d = btm_kaili_start_tds[1].text
    # 负
    btm_kaili_s_a = btm_kaili_start_tds[2].text

    # 即
    btm_kaili_end_tds = btm_kaili_trs[1].find_all("td")
    # 胜
    btm_kaili_e_h = btm_kaili_end_tds[0].text
    # 平
    btm_kaili_e_d = btm_kaili_end_tds[1].text
    # 负
    btm_kaili_e_a = btm_kaili_end_tds[2].text
    avgps = {"spf" :{"s": [btm_shp ,btm_sdp ,btm_sap], "e": [btm_ehp ,btm_edp ,btm_eap]},
             "gailv" :{"s": [btm_jisp_s_h ,btm_jisp_s_d ,btm_jisp_s_a], "e": [btm_jisp_e_h ,btm_jisp_e_d ,btm_jisp_e_a]},
             "kailv" :{"s" :[btm_kaili_s_h ,btm_kaili_s_d ,btm_kaili_s_a]
                       ,"e" :[btm_kaili_e_h ,btm_kaili_e_d ,btm_kaili_e_a]},
             "fanhuan" :{"s" :btm_fan_s ,"e" :btm_fan_e}}

    # 离散值
    ls_tds = btm_trs[-1].find_all("td" ,recursive=False)

    ls_spf_trs = ls_tds[1].find_all("tr")
    # 初赔
    ls_spf_start_tds = ls_spf_trs[0].find_all("td")
    # 胜
    ls_shp = ls_spf_start_tds[0].text
    # 平
    ls_sdp = ls_spf_start_tds[1].text
    # 负
    ls_sap = ls_spf_start_tds[2].text

    # 即赔
    ls_spf_end_tds = ls_spf_trs[1].find_all("td")
    # 胜.text
    ls_ehp = ls_spf_end_tds[0].text
    # 平
    ls_edp = ls_spf_end_tds[1].text
    # 负
    ls_eap = ls_spf_end_tds[2].text
    ls = {"s":[ls_shp ,ls_sdp ,ls_sap] ,"e" :[ls_ehp ,ls_edp ,ls_eap]}
    # avgps:平均欧赔，ls:离散值
    tj = {"avgps":avgps,"ls":ls}
    #  各个博彩公司的胜平负赔率，概率，返还率，凯利指数以及统计数据（平均欧赔的赔率，概率，返还率，离散值）,
    ouzhi = {"matchNo":matchNo,"fid":fid,"updateTime":datetime.datetime.now().timestamp(),"pslist":pslist,"tj":tj}
    return ouzhi